package clonetest;

public class CloneTest  implements Cloneable {

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        CloneTest cloneTest = new CloneTest();
        cloneTest.setUser(new User());
        CloneTest clone = cloneTest.clone();
        System.out.println(cloneTest);
        System.out.println(clone);


        System.out.println(cloneTest.getUser());
        System.out.println(clone.getUser());
        //深克隆 克隆出来的对象内部的成员变量内存地址不相等

    }

    @Override
    protected CloneTest clone() throws CloneNotSupportedException {
        CloneTest clone = (CloneTest)super.clone();
        clone.setUser(this.getUser().clone());
        return clone;
    }
}


class User implements Cloneable{
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    protected User clone() throws CloneNotSupportedException {
        return (User)super.clone();
    }
}
